package ui.JDialog;

import domain.Player;
import ui.LoginJFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/*
    * 负责人:zzx Aullre
    * 功能:特殊年弹窗
    * */

public class SpecialYearJDialog extends JDialog implements ActionListener {
    private JLabel eventLabel;
    private JButton option1, option2, option3;
    private Player player;
    private String[] effects;
    private String selectedOption = "";

    public SpecialYearJDialog(int age, String event, String[] options, String[] effects, Player player) {
        this.effects = effects;
        this.player = player;
        
        // 设置弹窗基本属性
        setTitle(age + "岁特殊事件");
        setSize(500, 300);
        //setLocationRelativeTo(null);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int x = screenSize.width-500;
        int y = screenSize.height-600;
        this.setLocation(x, y);
        setModal(true);
        setLayout(new BorderLayout());
        setAlwaysOnTop(true);
        //initImage();
        // 初始化界面
        initView(event, options);
    }
    //为弹窗添加背景会导致提示文字和按钮被覆盖,要鼠标滑过才会重绘按钮
  /*  private void initImage() {
        //添加背景图片
        JLabel background = new JLabel(new ImageIcon(this.getClass().getResource("/image/SpacialYearJDialog/特殊年弹窗背景3.png")));
        background.setBounds(0, 0, 500, 250);
        this.getContentPane().add(background);
    }
*/

    private void initView(String event, String[] options) {
        // 事件描述
        eventLabel = new JLabel("<html><body style='text-align:center;'>" + event + "</body></html>");
        eventLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        eventLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(eventLabel, BorderLayout.CENTER);

        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 20));
        
        // 创建选项按钮
        option1 = new JButton(options[0]);
        option1.addActionListener(this);
        buttonPanel.add(option1);

        if (options.length > 1) {
            option2 = new JButton(options[1]);
            option2.addActionListener(this);
            buttonPanel.add(option2);
        }

        if (options.length > 2) {
            option3 = new JButton(options[2]);
            option3.addActionListener(this);
            buttonPanel.add(option3);
        }

        add(buttonPanel, BorderLayout.SOUTH);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == option1) {
            Utils.Music.playSound("src\\music\\02.wav");
            selectedOption = option1.getText();
            handleEffect(0);
        } else if (option2 != null && e.getSource() == option2) {
            Utils.Music.playSound("src\\music\\02.wav");
            selectedOption = option2.getText();
            handleEffect(1);
        } else if (option3 != null && e.getSource() == option3) {
            Utils.Music.playSound("src\\music\\02.wav");
            selectedOption = option3.getText();
            handleEffect(2);
        }
        dispose();
    }

    private void handleEffect(int optionIndex) {
        String effect = effects[optionIndex];
        StringBuilder message = new StringBuilder("属性变化：\n");
        
        // 解析效果字符串并更新玩家属性
        String[] changes = effect.split("\\|");
        for (String change : changes) {
            String attribute = change.substring(0, change.length() - 2);
            int value = Integer.parseInt(change.substring(change.length() - 2));
            
            // 更新属性并记录变化
            switch (attribute) {
                case "iq":
                    player.setIq(player.getIq() + value);
                    message.append("智力: ").append(value > 0 ? "+" : "").append(value).append("\n");
                    break;
                case "pf":
                    player.setPf(player.getPf() + value);
                    message.append("体质: ").append(value > 0 ? "+" : "").append(value).append("\n");
                    break;
                case "fs":
                    player.setFs(player.getFs() + value);
                    message.append("颜值: ").append(value > 0 ? "+" : "").append(value).append("\n");
                    break;
                case "ffs":
                    player.setFfs(player.getFfs() + value);
                    message.append("家境: ").append(value > 0 ? "+" : "").append(value).append("\n");
                    break;
                case "luck":
                    player.setLuck(player.getLuck() + value);
                    message.append("运气: ").append(value > 0 ? "+" : "").append(value).append("\n");
                    break;
            }
        }
        
        // 显示属性变化提示
        JOptionPane.showMessageDialog(this, message.toString(), "属性变化", JOptionPane.INFORMATION_MESSAGE);
    }

    public String getSelectedOption() {
        return selectedOption;
    }

    // 所有特殊年事件的静态方法
    public static String showSixYearEvent(Player player) {
        String event = "幼儿园老师发现你擅长拼图：";
        String[] options = {"1. 专注培养动手能力", "2. 转向学习识字"};
        String[] effects = {"pf+2", "iq+2"};
        SpecialYearJDialog dialog = new SpecialYearJDialog(6, event, options, effects, player);
        dialog.setVisible(true);
        return dialog.getSelectedOption();
    }

    public static String showTwelveYearEvent(Player player) {
        String event = "初中分班时面临选择：";
        String[] options = {
            "1. 进入奥数竞赛班",
            "2. 加入体育特长班",
            "3. 留在普通班"
        };
        String[] effects = {"iq+3|pf-1", "pf+3|iq-1", "iq+1|pf+1"};
        SpecialYearJDialog dialog = new SpecialYearJDialog(12, event, options, effects, player);
        dialog.setVisible(true);
        return dialog.getSelectedOption();
    }

    public static String showEighteenYearEvent(Player player) {
        String event = "高考志愿填报：";
        String[] options = {
            "1. 选择顶尖名校冷门专业",
            "2. 选择普通院校热门专业",
            "3. 放弃升学创业"
        };
        String[] effects = {"iq+4|ffs-2", "iq+1|luck+2", "ffs+2|luck-1"};
        SpecialYearJDialog dialog = new SpecialYearJDialog(18, event, options, effects, player);
        dialog.setVisible(true);
        return dialog.getSelectedOption();
    }

    public static String showTwentyFiveYearEvent(Player player) {
        String event = "职场晋升机会：";
        String[] options = {
            "1. 接受996高压岗位",
            "2. 拒绝内卷跳槽",
            "3. 裸辞gap year"
        };
        String[] effects = {"iq+2|pf-3", "pf+2|ffs-1", "luck+1|ffs-2"};
        SpecialYearJDialog dialog = new SpecialYearJDialog(25, event, options, effects, player);
        dialog.setVisible(true);
        return dialog.getSelectedOption();
    }

    public static String showThirtyYearEvent(Player player) {
        String event = "生育决策：";
        String[] options = {
            "1. 丁克专注事业",
            "2. 生育一胎",
            "3. 收养孤儿"
        };
        String[] effects = {"iq+3|ffs+2", "pf-2|ffs+1|luck+1", "pf+1|luck+3|iq-1"};
        SpecialYearJDialog dialog = new SpecialYearJDialog(30, event, options, effects, player);
        dialog.setVisible(true);
        return dialog.getSelectedOption();
    }

    public static String showFortyYearEvent(Player player) {
        String event = "中年资产规划：";
        String[] options = {
            "1. 投资高风险股票",
            "2. 购买稳健理财",
            "3. 捐赠公益基金"
        };
        String[] effects = {"ffs+5|luck-4", "ffs+2|iq+1", "luck+3|ffs-2|pf+1"};
        SpecialYearJDialog dialog = new SpecialYearJDialog(40, event, options, effects, player);
        dialog.setVisible(true);
        return dialog.getSelectedOption();
    }

    public static String showFiftyYearEvent(Player player) {
        String event = "健康转折点：";
        String[] options = {
            "1. 开始高强度健身",
            "2. 选择养生佛系生活",
            "3. 忽视健康继续熬夜"
        };
        String[] effects = {"pf+4|iq-2", "pf+1|iq+2", "pf-5|iq-1"};
        SpecialYearJDialog dialog = new SpecialYearJDialog(50, event, options, effects, player);
        dialog.setVisible(true);
        return dialog.getSelectedOption();
    }

    public static String showSixtyYearEvent(Player player) {
        String event = "退休生活选择：";
        String[] options = {
            "1. 环球旅行",
            "2. 帮子女带娃",
            "3. 重返职场返聘"
        };
        String[] effects = {"pf+2|luck+3", "ffs+2|pf-1", "iq+2|ffs-1"};
        SpecialYearJDialog dialog = new SpecialYearJDialog(60, event, options, effects, player);
        dialog.setVisible(true);
        return dialog.getSelectedOption();
    }

    public static String showSeventyYearEvent(Player player) {
        String event = "老年兴趣抉择：";
        String[] options = {
            "1. 学习绘画书法",
            "2. 参加广场舞队",
            "3. 研究投资理财"
        };
        String[] effects = {"iq+3|fs+2", "pf+2|luck+1", "ffs+2|iq-1"};
        SpecialYearJDialog dialog = new SpecialYearJDialog(70, event, options, effects, player);
        dialog.setVisible(true);
        return dialog.getSelectedOption();
    }

    public static String showEightyYearEvent(Player player) {
        String event = "遗产分配思考：";
        String[] options = {
            "1. 全部留给子女",
            "2. 设立慈善基金",
            "3. 挥霍享受余生"
        };
        String[] effects = {"ffs+1|luck-2", "luck+4|ffs-3", "pf-1|ffs-5|iq+1"};
        SpecialYearJDialog dialog = new SpecialYearJDialog(80, event, options, effects, player);
        dialog.setVisible(true);
        return dialog.getSelectedOption();
    }
}
